package com.tool.weixin.api;

import net.sf.json.JSONObject;

import org.slf4j.Logger;

import com.tool.weixin.message.BaseMessage;
import com.tool.weixin.util.HttpUtils;

/**
 * 客服接口
 * @author huff
 *
 */
public class CustomApi {
	
	private static final Logger logger = org.slf4j.LoggerFactory.getLogger(CustomApi.class);

	/**发送消息**/
	private static final String CUSTOM_MESSAGE_SEND = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=ACCESS_TOKEN";

	/**
	 * 发送客服消息
	 * @param msg
	 */
	public static int semdMsg(BaseMessage msg) {
		int result = 0;
		String requestUrl = CUSTOM_MESSAGE_SEND.replace("ACCESS_TOKEN", AccessTokenApi.getAccessToken().getToken());
        JSONObject jsonObject =HttpUtils.httpRequest(requestUrl, "POST", msg.tojson());
        if (null != jsonObject) {
			if (0 != jsonObject.getInt("errcode")) {
				result = jsonObject.getInt("errcode");
				logger.error("发送客服消息 errcode:{} errmsg:{}", jsonObject.getInt("errcode"), jsonObject.getString("errmsg"));
			}
		}
		return result;
	}
	
}
